package com.fushenlan.commons;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.beans.factory.annotation.Value;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author PC
 * @title: GeneratorUtil
 * @projectName SpringBootDemo
 * @description: TODO
 * @date 2022/1/14 10:38
 */
@Data
@Slf4j
public class GeneratorUtil {

    /**
     * 判断小数点后2位数字的正则表达式(正负数都可)
     *
     * @param str
     * @return
     */
    public static boolean isNumber(String str) {
        //判断
        Pattern pattern = Pattern.compile("^[-]?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){2,2})?$");
        Matcher matcher = pattern.matcher(str);
        if (matcher.matches() == false) {
            return false;
        } else {
            return true;
        }

    }

    /**
     * 获取32位的唯一流水号 uuid
     */
    public static String generateUuid() {
        String uuid = UUID.randomUUID().toString();
        String[] strArray = uuid.split("\\s*[-]\\s*");
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < strArray.length; i++) {
            stringBuffer.append(strArray[i]);

        }
        uuid = stringBuffer.toString();
        return uuid;
    }

    /**
     * 加密
     *
     * @param str
     * @return
     */
    public String encrypt(String str) {
        //获取默认加密类
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        //盐，对应启动项上salt， 默认nacos的groupid
        encryptor.setPassword("dev");
        return encryptor.encrypt(str);
    }

    /**
     * 解密
     *
     * @param str
     * @return
     */
    public String decrypt(String str) {
        //获取默认加密类
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        //盐，对应启动项上salt， 默认nacos的groupid
        encryptor.setPassword("dev");
        return encryptor.decrypt(str);
    }

    @Value("jasypt.encryptor.password")
    private String salt;

    /**
     * 获取程序jvm中的配置
     */
    public String getProperties(String property) {

        return property;
    }

    /**
     * 判断时间格式 格式必须为“YYYY-MM-dd”  * 2004-2-30 是无效的  * 2003-2-29 是无效的  * @param sDate  * @return
     */
    public static boolean isValidDate(String str) {
        //String str = "2007-01-02";
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = (Date) formatter.parse(str);
            //DebugPrintf.Printf(str);
            //DebugPrintf.Printf(formatter.format(date));
            System.out.println("str-->" + str);
            System.out.println("formatter.format(date)-->" + formatter.format(date));
            return str.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 测试
     */
    public static void main(String[] args) {
        GeneratorUtil generatorUtil = new GeneratorUtil();
        boolean bool = isNumber("11.20");
        System.out.println("bool---->" + bool);
        String uuid = generateUuid();
        System.out.println("uuid==>" + uuid);
        //加密
        System.out.println("密码加密：" + generatorUtil.encrypt("root"));

        //解密
        System.out.println("密码解密：" + generatorUtil.decrypt("A4PXemKpbSeNqbzRF+B2YQ=="));

        //获取加密 盐的 配置属性 在工具类中无法获取，不是一个启动进程
        System.out.println("盐==》" + generatorUtil.getProperties(generatorUtil.getSalt()));

        //校验日期 YYYYY-MM-DD
        boolean boolDate = GeneratorUtil.isValidDate("2021-01-311");
        System.out.println("boolDate-->" + boolDate);


    }
}
